﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Net5ApiAndEfMySql.DtoModel.Common;
using Net5ApiAndEfMySql.DtoModel.SystemAdmin;
using Net5ApiAndEfMySql.IService.SystemAdmin;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Net5ApiAndEfMySql.ApiProject.Controllers
{
   
    public class AdminUserManagerController : BaseApiController
    {
        private IAdminUserService _iAdminUserService;
        public AdminUserManagerController(IAdminUserService iAdminUserService)
        {
            _iAdminUserService = iAdminUserService;
        }
        [AllowAnonymous]
        [HttpPost]
        public ResultModel Login(LoginModel login)
        {
            ResultModel result = new ResultModel();
            if (login == null)
            {
                result.code = ResultCode.Error;
                result.mess = "参数错误";
                return result;
            }
            if (string.IsNullOrEmpty(login.username) || string.IsNullOrEmpty(login.password))
            {
                result.code = ResultCode.Error;
                result.mess = "参数错误";
                return result;
            }
            try
            {
                string mess = "";
                AdminUserDto user = _iAdminUserService.Login(login.username, login.password, login.clientType, out mess);
                if (user != null)
                {
                    result.code = ResultCode.Ok;
                    result.data = new { token = user.Token, userData = user };

                }
                else
                {
                    result.code = ResultCode.Fail;
                    result.data = null;
                }

                result.mess = mess;
            }
            catch (Exception ex)
            {

                result.code = ResultCode.Error;
                result.mess = ex.ToString();
            }
            return result;
        }
        [HttpPost]
        public ResultModel GetPageList(SearchModel m)
        {
            ResultModel result = new ResultModel();
            if (m == null)
            {
                result.code = ResultCode.Error;
                result.mess = "参数错误";
                return result;
            }
            try
            {
                var data = _iAdminUserService.GetPageList(m);

                data.draw = m.draw;
                result.code = ResultCode.Ok;
                result.data = data;
                result.mess = "成功!";
            }
            catch (Exception ex)
            {
                result.code = ResultCode.Error;
                result.mess = "错误!";
            }
            return result;

        }



        public ResultModel GetUserInfo(string token)
        {
            ResultModel result = new ResultModel();

            if (string.IsNullOrEmpty(token))
            {
                result.code = ResultCode.Error;
                result.mess = "参数错误";
                return result;
            }
            try
            {

                result.code = ResultCode.Ok;
                result.data = new AdminUserDto() { Token = "test_token" };
                result.mess = "成功!";
            }
            catch (Exception ex)
            {
                result.code = ResultCode.Error;
                result.mess = "错误!";
            }
            return result;
        }


        
        [AllowAnonymous]
        public ResultModel LoginOut()
        {
            ResultModel result = new ResultModel();
           
            if (string.IsNullOrEmpty(Token))
            {
                result.code = ResultCode.Error;
                result.mess = "您没有登录";
                return result;
            }
            try
            {
                if (_iAdminUserService.LoginOut(Token))
                {
                    result.code = ResultCode.Ok;
                    result.data = "退出成功";
                    result.mess = "成功!";
                }
                else
                {
                    result.code = ResultCode.Fail;
                    result.data = "退出失败";
                    result.mess = "失败!";
                }
               
            }
            catch (Exception ex)
            {
                result.code = ResultCode.Error;
                result.mess = "错误!";
            }
            return result;
        }


        public ResultModel ValidateUserName(string userName)
        {
            ResultModel result = new ResultModel();
            if (string.IsNullOrEmpty(userName))
            {
                result.code = ResultCode.Error;
                result.data = false;
                result.mess = "参数错误";
                return result;
            }
            if (_iAdminUserService.ValidateUserName(userName))
            {
                result.code = ResultCode.Ok;
                result.data = true;
                result.mess = "用户名可以使用";
            }
            else
            {
                result.code = ResultCode.Ok;
                result.data = false;
                result.mess = "用户名已经被使用";
            }
            return result;

        }


        [HttpPost]
        public ResultModel AdminUserAdd(AdminUserDto user)
        {
            ResultModel result = new ResultModel();
            if (string.IsNullOrWhiteSpace(user.user_name))
            {
                result.code = ResultCode.Error;
                result.mess = "必须输入用户名";
                return result;
            }
            try
            {

                var resultData = _iAdminUserService.AdminUserAdd(user);
                if (resultData)
                {
                    result.code = ResultCode.Ok;
                    result.data = resultData;
                    result.mess = "成功!";
                }
                else {
                    result.code = ResultCode.Fail;
                    result.data = resultData;
                    result.mess = "添加失败!";
                }
              
            }
            catch (Exception ex)
            {
                result.code = ResultCode.Error;
                result.mess = "错误!";
            }
            return result;
        }
        [HttpPost]
        public ResultModel AdminUserUpdate(AdminUserDto user)
        {
            ResultModel result = new ResultModel();
            if (string.IsNullOrWhiteSpace(user.user_name))
            {
                result.code = ResultCode.Error;
                result.mess = "必须输入用户名";
                return result;
            }
            try
            {
                var resultData = _iAdminUserService.AdminUserUpdate(user);
                result.code = ResultCode.Ok;
                result.data = resultData;
                result.mess = "成功!";
            }
            catch (Exception ex)
            {
                result.code = ResultCode.Error;
                result.mess = "错误!";
            }
            return result;
        }
        public ResultModel GetAdminUserByUserName(string userName)
        {
            ResultModel result = new ResultModel();
            if (string.IsNullOrWhiteSpace(userName))
            {
                result.code = ResultCode.Error;
                result.mess = "参数错误";
                return result;
            }
            try
            {
                var data = _iAdminUserService.GetAdminUserDtoByUserName(userName);
                result.code = ResultCode.Ok;
                result.data = data;
                result.mess = "成功!";
            }
            catch (Exception ex)
            {
                result.code = ResultCode.Error;
                result.mess = "错误!";
            }
            return result;
        }


    }
    public class LoginModel
    {
        public string username { get; set; }
        public string password { get; set; }

        public ClientType clientType { get; set; }
    }
}
